﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using YiSha.Util;
using YiSha.Util.Model;
using YiSha.Entity;
using YiSha.Model;
using YiSha.Admin.Web.Controllers;
using YiSha.Entity.YiDongYuan;
using YiSha.Business.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Web.Code;

namespace YiSha.Admin.Web.Areas.YiDongYuan.Controllers
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-09 12:00
    /// 描 述：检测详细信息控制器类
    /// </summary>
    [Area("YiDongYuan")]
    public class DetectionInfoController : BaseController
    {
        private DetectionInfoBLL detectionInfoBLL = new DetectionInfoBLL();
        private DetectionTaskBLL detectionTaskBLL = new DetectionTaskBLL();
        private DetectionMechanicsBLL detectionMechanicsBLL = new DetectionMechanicsBLL();
        private DetectionResultBLL detectionResultBLL = new DetectionResultBLL();

        #region 视图功能

        [AuthorizeFilter("yidongyuan:detectioninfo:view")]
        public ActionResult DetectionInfoIndex()
        {
            return View();
        }

        public ActionResult DetectionInfoForm()
        {
            return View();
        }

        /// <summary>
        /// 根据TaskId获取检测报告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult CMAReport([FromQuery] long id = 0, [FromQuery] string code = "")
        {
            //根据taskId查询DetectionTask
            DetectionTaskEntity task;
            if (id > 0)
            {
                task = detectionTaskBLL.GetEntity(id).Result.Data;
            }
            else
            {
                task = detectionTaskBLL.GetLastEntityByCode(code).Result.Data;
            }
            ViewBag.Task = task;

            //根据MechanicsId查询机械信息--DetectionMechanics
            DetectionMechanicsEntity mechanics = detectionMechanicsBLL.GetEntity(task.MechanicsId.Value).Result.Data;
            ViewBag.Mechanics = mechanics;

            //根据TaskId查询DetectionInfo
            DetectionInfoEntity info = detectionInfoBLL.GetEntity(task.DetectionInfoId.Value).Result.Data;
            ViewBag.Info = info;

            //根据ResultDataId查询DetectionResult
            DetectionResultEntity result = detectionResultBLL.GetEntity(info.ResultDataId.Value).Result.Data;
            ViewBag.Result = result;
            return View();
        }

        #endregion 视图功能

        #region 获取数据

        [HttpGet]
        [AuthorizeFilter("yidongyuan:detectioninfo:search")]
        public async Task<ActionResult> GetListJson(DetectionInfoListParam param)
        {
            TData<List<DetectionInfoEntity>> obj = await detectionInfoBLL.GetList(param);
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("yidongyuan:detectioninfo:search")]
        public async Task<ActionResult> GetPageListJson(DetectionInfoListParam param, Pagination pagination)
        {
            TData<List<DetectionInfoEntity>> obj = await detectionInfoBLL.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<ActionResult> GetFormJson(long id)
        {
            TData<DetectionInfoEntity> obj = await detectionInfoBLL.GetEntity(id);
            return Json(obj);
        }

        #endregion 获取数据

        #region 提交数据

        [HttpPost]
        [AuthorizeFilter("yidongyuan:detectioninfo:add,yidongyuan:detectioninfo:edit")]
        public async Task<ActionResult> SaveFormJson(DetectionInfoEntity entity)
        {
            OperatorInfo user = await Operator.Instance.Current();
            TData<string> obj = await detectionInfoBLL.SaveForm(entity, user);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("yidongyuan:detectioninfo:delete")]
        public async Task<ActionResult> DeleteFormJson(string ids)
        {
            TData obj = await detectionInfoBLL.DeleteForm(ids);
            return Json(obj);
        }

        #endregion 提交数据
    }
}
